package com.mrjy.queries.bo;

import com.mrjy.common.dto.QuestionCommentDto;
import com.mrjy.common.entity.*;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.coursepackage.dao.ICoursePackageInfoDao;
import com.mrjy.coursepackage.dao.IUserCoursePackageDao;
import com.mrjy.coursepackage.dto.CoursePackageInfoDTO;
import com.mrjy.custom.dao.ICustomModuleDao;
import com.mrjy.custom.dto.CustomModuleDto;
import com.mrjy.queries.dao.IQuestionInfoDao;
import com.mrjy.queries.dao.IQuestionTopicInfoDao;
import com.mrjy.queries.dao.IQuestionTypeRelDao;
import com.mrjy.video.dao.IVideoTypeDao;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("questionTopicInofBo")
public class QuestionTopicInofBo implements IQuestionTopicInfoBo {
	
	@Resource
	private IQuestionTopicInfoDao questionTopicInfoDao;
    @Resource
    private IQuestionTypeRelDao iQuestionTypeRelDao;
    @Resource
    private IVideoTypeDao iVideoTypeDao;
    @Resource
    private ICustomModuleDao customModuleDao;
    @Resource
    private ICoursePackageInfoDao coursePackageInfoDao;
    @Resource
    private IQuestionInfoDao questionInfoDao;
    @Resource
    private IUserCoursePackageDao userCoursePackageDao;

    @Override
    public Object queryListTopicInfo() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<QuestionTopicInfo> list = questionTopicInfoDao.queryListTopicInfo();
		if(null != list && list.size() > 0){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("rows", list);
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "查询无数据");
			map.put("rows", null);
		}
		return map;
	}

	@Override
	public Object queryById(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		QuestionTopicInfo topicInfo = questionTopicInfoDao.selectByPrimaryKey(id);
		if(null != topicInfo){
			int total = questionTopicInfoDao.countQuestionsByTopicId(id);
			int anwserNum = questionTopicInfoDao.countQuestionAnwserByTopicId(id);
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("info", topicInfo);
			map.put("total", total);
			map.put("anwserNum", anwserNum);
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "查询五数据");
		}
		return map;
	}

	@Override
	public Object queryListTopicInfoForEasyUI() {
		return questionTopicInfoDao.queryListTopicInfo();
	}

    @Override
    public Object queryListCustomTopicInfo(CustomModuleDto customModuleDto) {
        Map<String, Object> map = new HashMap<String, Object>();
        int total = questionTopicInfoDao.countCustomTopicInfo(customModuleDto);
        if (total > 0) {
            List<QuestionTopicInfo> list = questionTopicInfoDao.queryListCustomTopicInfo(customModuleDto);
            map.put("err_code", 0);
            map.put("err_msg", "查询成功");
            map.put("rows", list);
            map.put("total", total);
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "查询无数据");
            map.put("rows", null);
            map.put("total", total);
        }
        return map;
    }

    @Override
    public Object countByCustomId(CustomModule customModule) {
        Map<String, Object> map = new HashMap<String, Object>();

        CustomModuleDto customModuleDto = (CustomModuleDto) customModuleDao.selectByPrimaryKey(customModule.getId());
        CoursePackageInfoDTO dto = new CoursePackageInfoDTO();
        dto.setUserId(customModule.getUserId());
        dto.setCustomTypeId(customModule.getId());
        QuestionInfo questionInfo = new QuestionInfo();
        questionInfo.setCustomTypeId(customModule.getId());
        questionInfo.setGoodUserId(customModule.getUserId());
        questionInfo.setQuestionTime(new Timestamp(System.currentTimeMillis()));
        UserCoursePackageRel userCoursePackageRel = new UserCoursePackageRel();
        userCoursePackageRel.setCustomModuleId(customModule.getId());

        int classmateNum = userCoursePackageDao.countClassmate(userCoursePackageRel);

        //当前用户的viewprogress
        userCoursePackageRel.setUserId(customModule.getUserId());
        List<UserCoursePackageRel> userCoursePackageRels = userCoursePackageDao.queryClassmate(userCoursePackageRel);
        if (userCoursePackageRels == null || userCoursePackageRels.size() == 0) {
        } else {
            UserCoursePackageRel userCoursePackageRel1 = userCoursePackageRels.get(0);
            customModuleDto.setViewProgress(userCoursePackageRel1.getViewProgress());
        }
        int parentCert = customModuleDao.queryParentCertificationByUserId(customModuleDto);
        map.put("parentCert", parentCert);
        map.put("classmateNum", classmateNum);
        map.put("course_num", coursePackageInfoDao.countPageByCustomType(dto));
        map.put("err_code", 0);
        map.put("err_msg", "查询成功");
        map.put("info", customModuleDto);
        map.put("QAnum", questionInfoDao.countTopicPageByInfo(questionInfo));
        return map;
    }
    
    @Override
	public Object drawTopic(CustomModuleDto module, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
        UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
//        if (null == loginUser) {
//            map.put("err_code", -1);
//            map.put("err_msg", "用户未登录");
//            return map;
//        }

        QuestionInfo questionInfo = new QuestionInfo();
        questionInfo.setCustomTypeId(module.getId());
        questionInfo.setGoodUserId(module.getUserId());
        questionInfo.setQuestionTime(new Timestamp(System.currentTimeMillis()));
        questionInfo.setPage(module.getPage());
        questionInfo.setRows(module.getRows());

        UserCoursePackageRel userCoursePackageRel = new UserCoursePackageRel();
        userCoursePackageRel.setCustomModuleId(module.getId());
        /*
         * 相关课程
         */
        if (module.getSortType().equals("relatedCP")) {
        	CoursePackageInfoDTO dto = new CoursePackageInfoDTO();
        	dto.setUserId(module.getUserId());
        	dto.setCustomTypeId(module.getId());
        	dto.setPage(module.getPage());
        	dto.setRows(module.getRows());
			List<CoursePackageInfoDTO> list = coursePackageInfoDao.queryPageByCustomType(dto);
			map.put("err_code", 200);
        	map.put("err_msg", "查询成功");
        	map.put("rows", list);
		}
        /*
         * 交流
         */
        else if (module.getSortType().equals("communication")) {
        	List<QuestionCommentDto> list = questionInfoDao.queryTopicPageByInfo(questionInfo);
        	map.put("err_code", 200);
        	map.put("err_msg", "查询成功");
        	map.put("rows", list);
		}
        /*
         *同学
         */
        else if (module.getSortType().equals("classmate")) {
            userCoursePackageRel.setPage(module.getPage());
            userCoursePackageRel.setRows(module.getRows());
            userCoursePackageRel.setUserId(null);
            List<UserCoursePackageRel> list = userCoursePackageDao.queryClassmate(userCoursePackageRel);
            map.put("err_code", 200);
            map.put("err_msg", "查询成功");
            map.put("rows", list);
        }
		return map;
	}
}
